import time
from selenium import webdriver
from selenium.webdriver.common.by import By

#
# 任务1：
# A3mall 登录搜索并下单
driver=webdriver.Edge()
driver.maximize_window()
driver.get('http://10.59.9.12:7001/')
# 1/点击登录  /html/body/div[1]/div/div[2]/ul/li[1]/a
driver.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/ul/li[1]/a').click()
time.sleep(3)
# 2/断言1  会员登录
dy1=driver.find_element(By.XPATH,'/html/body/div[4]/div/div[1]/div[1]/span[1]').text
print(dy1)
time.sleep(3)
# 3/填写用户名
driver.find_element(By.XPATH,'//input[@class="regi-input" and @name="username"]').send_keys('13811033962')
# driver.find_element(By.NAME,'username').send_keys('13811033962')
time.sleep(3)
# 4/填写密码
driver.find_element(By.NAME,'password').send_keys('123456')
time.sleep(10)
# 5/登录按钮
driver.find_element(By.XPATH,'//*[@id="theForm"]/div[5]/a').click()
time.sleep(3)
# 6/断言  欢迎回来   /html/body/div[1]/div/div[2]/ul/li[1]/a
dy2=driver.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/ul/li[1]/a').text
print(dy2)
time.sleep(3)
# 6/搜索框   //*[@id="logo"]/a/img
driver.find_element(By.XPATH,'//*[@id="searchForm"]/div[1]/input').send_keys("非洲之心")
time.sleep(3)
# 7/搜索按钮 search-input-btn
driver.find_element(By.CLASS_NAME,'search-input-btn').click()
time.sleep(3)
# 8/商品详情
driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div[2]/div[1]/ul/li/div[1]/span/a/img').click()
time.sleep(3)
# 9/立即购买
driver.find_element(By.CSS_SELECTOR,'.shop-btn').click()
time.sleep(3)
# 10/  断言   确认订单    /html/body/div[4]/div[1]/a[2]
dy3=driver.find_element(By.XPATH,'/html/body/div[4]/div[1]/a[2]').text
print(dy3)
time.sleep(5)
# 10/ 确认收货地址    (高阶  方法1)
driver.find_element(By.XPATH,'//input[starts-with(@class,"radio-select-btn")]').click()
time.sleep(3)
# driver.find_element(By.ID,'address-801').click()
# time.sleep(3)
# 11/ 下一步    (高阶  方法2)
driver.find_element(By.XPATH,'//a[contains(@id,"order-button")]').click()
time.sleep(3)
# 12/ 查看订单  /html/body/div[4]/div[3]/p/a
driver.find_element(By.XPATH,'/html/body/div[4]/div[3]/p/a').click()
time.sleep(3)


# /html/body/div[4]/div/div[2]/div[2]/p/a[1]
#
# //*[@id="wd"]


#
# 任务2：
# 百度页面(切换页面)
# driver.get('https://www.baidu.com//')
# driver.find_element(By.XPATH,'//*[@id="nv"]/a[1]').click()
# time.sleep(3)
# driver.switch_to.window(driver.window_handles[-1])
# driver.find_element(By.PARTIAL_LINK_TEXT,'两山').click()
# time.sleep(10)


# 知识点
from selenium.webdriver import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains

# 断言  会员登录
dy1=driver.find_element(By.XPATH,'/html/body/div[4]/div/div[1]/div[1]/span[1]').text
print(dy1)
time.sleep(3)

# 切换窗口
driver.switch_to.window(driver.window_handles[-1])
time.sleep(10)

# 进入框架
driver.switch_to.frame(driver.find_element(By.ID,'iframe_login'))
time.sleep(3)
# 退出框架
driver.switch_to.default_content()

# 等待
# 强制等待 1
time.sleep(3)
# 隐式等待 2
driver.implicitly_wait(1)
# 显示等待
WebDriverWait(driver,20,0.5).until(EC.presence_of_element_located((By.ID,'save'))).click()


#获取所有窗口
hq=driver.find_element(By.XPATH,'/html/body/div[4]/div[3]/p/a')
time.sleep(3)
#打印所有窗口
print(hq)
#退出浏览器
driver.close()

# 鼠标操作
sbcz=driver.find_element(By.CLASS_NAME,'headerLogin')
time.sleep(3)
# 单击
ActionChains(driver).click(sbcz).perform()
# 双击
ActionChains(driver).double_click(sbcz).perform()

# 键盘操作事件
a=driver.find_element(By.NAME,'uname')
a.send_keys("13103053968")
a.send_keys(Keys.ENTER)
time.sleep(3)

# 滑动
driver.execute_script('window.scrollTo(0,800)')
time.sleep(5)

# 向下滚动1000像素
driver.execute_script('window.scrollTo(0,1000)')
time.sleep(2)


# 滚动到底部
driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')
time.sleep(2)
# 滚动到顶部
driver.execute_script('widnow.scrollTo(0,0)')
time.sleep(10)

# 5种定位元素的方式
driver.find_element(By.XPATH,'//a[@href="/home/login.html"]')
driver.find_element(By.CLASS_NAME,'reg-new')
driver.find_element(By.NAME,'password').send_keys("123456")
driver.find_element(By.ID,'buy-cart').click()
driver.find_element(By.CSS_SELECTOR,'.ask-btn').click()